package models.games;

import java.util.Random;

import core.User;

public class ConfrontoRapido extends Game {
	private static final int GAME_TIME = 60; // secondi durata partita
	private static final int CORRECT_ANSWERS_TO_INCREMENT_MULTIPLIER = 4;
	public static final int NUMBER_OF_IMAGES = 6;

	private int correctCounter; // contatore risposte esatte, si azzera ogni 4 e
								// aumenta il coefficiente moltiplicatore del
								// punteggio
	private int scoreMultiplier;// moltiplicatore del punteggio (default x1)

	private int time = GAME_TIME; // il tempo attuale espresso in secondi 0 <
									// time <
	// GAME_TIME

	// un immagine è vista come un intero da 0 a NUMBER_OF_IMAGES, -1 == null
	private int previousImage;
	private int currentImage;
	private Random random;

	public ConfrontoRapido(User user) {
		super(user);
		random = new Random();
		correctCounter = 0;
		scoreMultiplier = 1;
		previousImage = -1;
		currentImage = random.nextInt(NUMBER_OF_IMAGES);
	}

	/**
	 * answer true se ha risposto si (le due immagini corrispondono) risposta
	 * corretta
	 * 
	 * @param answer
	 */
	public boolean elaborateAnswer(boolean answer) {
		boolean correct = false;
		if (answer == (previousImage == currentImage)) {
			System.out.println("Risposta corretta");
			setScore(getScore() + 250 * scoreMultiplier);
			correctCounter++;
			if (correctCounter >= CORRECT_ANSWERS_TO_INCREMENT_MULTIPLIER) {
				correctCounter = 0;
				scoreMultiplier++;
			}
			correct = true;
		} else {
			System.out.println("Risposta errata");
			correctCounter = 0;
			scoreMultiplier = 1;
			correct = false;
		}
		int newRandom = random.nextInt(NUMBER_OF_IMAGES * 2 - 1);
		if (newRandom >= NUMBER_OF_IMAGES) {
			previousImage = currentImage;
		} else {
			previousImage = currentImage;
			currentImage = newRandom;
		}
		return correct;
	}

	public int getCorrectCounter() {
		return correctCounter;
	}

	public int getScoreMultiplier() {
		return scoreMultiplier;
	}

	public int getCurrentImage() {
		return currentImage;
	}

	public int getTime() {
		return time;
	}

	public int decrementTime() {
		return time--;
	}

	public void firstRound() {
		previousImage = currentImage;
		currentImage = random.nextInt(NUMBER_OF_IMAGES);
	}

	@Override
	public int getGameKey() {
		return 1;
	}

}
